package com.example.liverock.androidalltest;

import android.app.Activity;

import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.media.Image;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class ListActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        /**
         * 功能：通过数组（图片和字符串），实现带有图标和文字的ListView
         */
        ListView listView = (ListView)findViewById(R.id.listView);
        // 获得数组的图片
        TypedArray image = getResources().obtainTypedArray(R.array.image);
        int[] imageId = new int[image.length()];
        // 获得数组图片对应的Id
        for (int i = 0; i < image.length(); i++)
        {
            imageId[i] = image.getResourceId(i,0);
        }
        // 获得数组的字符串
        String[] title = getResources().getStringArray(R.array.title);

        List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>();
        // 建立map关联，放进List列表里边
        for (int i = 0; i < imageId.length; i++)
        {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("image", imageId[i]);
            map.put("title", title[i]);
            listItems.add(map);
        }
        // 适配器， 注意 String[] 的用法，它们分别是map的Key；
        SimpleAdapter adapter = new SimpleAdapter(this, listItems, R.layout.list_item,
                new String[]{"image", "title"}, new int[]{R.id.image, R.id.title});
        listView.setAdapter(adapter);

    }


}
